package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.pojo.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author shi
 * @version v1.0
 * @date 2019/8/7 21:37
 * @description TODO
 **/
@Service(interfaceClass = RoleSettingService.class)
@Transactional
public class RoleSettingServiceImpl implements RoleSettingService {


    @Autowired
    RoleDao roleDao;

    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {
        /**
         *  @描述：分页查询
         */
        PageHelper.startPage(currentPage,pageSize);
        Page<Role> menus =  roleDao.findPage();
        return new PageResult(menus.getTotal(),menus.getResult());
    }



    @Override
    public void addRole(Role role, Integer[] permissionIds, Integer[] menuIds) {


        roleDao.addRole(role);

        roleDao.addRoleAndPermissionIds(role.getId(),permissionIds);

        roleDao.addRoleAndMenuIds(menuIds);

    }

    @Override
    public void edit(Role role, Integer[] permissionIds, Integer[] menuIds) {
        roleDao.edit(role);

        roleDao.deletePermission(role.getId());
        roleDao.addRoleAndPermissionIds(role.getId(),permissionIds);

        roleDao.deleteMenuIds(role.getId());

        roleDao.addRoleAndMenuIds(menuIds);
    }

    @Override
    public void delete(Integer id) {
        roleDao.deleteByPrimaryKey(id);
        roleDao.deleteMenuIds(id);
        roleDao.deletePermission(id);
    }

    @Override
    public Role findRoleByID(Integer id) {

        return roleDao.findRoleByID(id);
    }

    @Override
    public List<Role> findAll() {
        return roleDao.findAll();
    }

    @Override
    public Integer[] findMenuById(Integer id) {
        return roleDao.findMenuById(id);
    }
    @Override
    public Integer[] findPermissionById(Integer id) {
        return roleDao.findPermissionById(id);
    }
}
